package com.huawei.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CustomDataBaseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        // 获取分片列的值
        String id = shardingValue.getValue();

        // 自定义分片逻辑
        // 例如：根据 id 的哈希值取模，决定分片
        int hash = id.hashCode();
        int index = Math.abs(hash) % availableTargetNames.size();
        // 返回目标库名
        System.out.println("index:" + index);
        return "m" + index;
    }

}